package com.eboss2.sbs.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.chat.ChatMainActivity;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.TabBasicActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.MySbsInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.eboss2.sbs.weight.DialogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hxchat.activity.MyChatActivity;
import com.hxchat.activity.MyConversationListActivity;
import com.hxchat.utils.APPConfig;
import com.hxchat.utils.SharedPreferencesUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseConstant;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.SystemUtil;
import com.mytools.tool.views.MyDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tool.utils.utils.LogUtils;
import com.tool.utils.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class MineActivity extends TabBasicActivity implements View.OnClickListener{


	private static final String TAG = "MineActivity";
	private ImageView img_user_icon;
	private TextView tv_name;
	private TextView tv_time;
	private TextView tv_balance_amt;
	private TextView tv_point_amt;
	private TextView tv_version;

	private long groupId;
	private long userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.fragment_mine_setting);
		MyActivityManager.getInstance().addActivity(this);
//		StatusBarUtil.setColor(this, getResources().getColor(R.color.statusBar), 0);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "我的商博士");
		showView(R.id.next_sure, false);
//		setTvText(R.id.next_sure, "设置");
		
//		img_user_icon = (ImageView) findViewById(R.id.img_user_icon);
//		ImageLoader.getInstance().displayImage(mApp.getLoginEntity().getMerchantIconUrl(), img_user_icon);
//		setTvText(R.id.tv_username, mApp.getLoginEntity().getName());

//		findViewById(R.id.layout_myinfo).setOnClickListener(this);
//		findViewById(R.id.layout_terminals).setOnClickListener(this);
//		findViewById(R.id.layout_barcodes).setOnClickListener(this);
//		findViewById(R.id.layout_mydatas).setOnClickListener(this);
//		findViewById(R.id.layout_wallet).setOnClickListener(this);
//		findViewById(R.id.layout_feedback).setOnClickListener(this);
//		findViewById(R.id.next_sure).setOnClickListener(this);


		relativeLayout(R.id.rl_smrz).setVisibility(View.GONE);
		relativeLayout(R.id.rl_bank_manager).setVisibility(View.GONE);





		linearLayout(R.id.ll_user_info).setOnClickListener(this);
		relativeLayout(R.id.rl_smrz).setOnClickListener(this);
		relativeLayout(R.id.rl_bank_manager).setOnClickListener(this);
		relativeLayout(R.id.rl_balance).setOnClickListener(this);
		relativeLayout(R.id.rl_jf_balance).setOnClickListener(this);
		relativeLayout(R.id.rl_device).setOnClickListener(this);
		relativeLayout(R.id.rl_message).setOnClickListener(this);
		relativeLayout(R.id.rl_operator).setOnClickListener(this);
		relativeLayout(R.id.rl_custom).setOnClickListener(this);
		relativeLayout(R.id.rl_qr).setOnClickListener(this);
		relativeLayout(R.id.rl_profit).setOnClickListener(this);
		relativeLayout(R.id.rl_login_setting).setOnClickListener(this);
		findViewById(R.id.rl_setting).setOnClickListener(this);
		img_user_icon = imageView(R.id.iv_img_header);
		tv_name = textView(R.id.tv_name);
		tv_time = textView(R.id.tv_time);
		tv_balance_amt = textView(R.id.tv_balance_amt);
		tv_point_amt = textView(R.id.tv_point_amt);
//		tv_version = textView(R.id.tv_version);
		setTvText(R.id.tv_version, "版本号："+ SystemUtil.getVerName(mContext));

	}


	@Override
	protected void onResume() {
		super.onResume();
		getdata();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ll_user_info:
				startActivity(new Intent(mContext, MyInfoActivity.class).putExtra("groupId", groupId).putExtra("userId", userId));
				break;
			case R.id.rl_smrz: //实名认证
				startActivity(new Intent(mContext, ActivitySmrz.class));
				break;
			case R.id.rl_bank_manager: //银行卡管理
				if (DialogUtils.checkApproveStatus(mContext)){
					return;
				}
				startActivity(new Intent(mContext, ActivityBankCardManager.class));
				break;
			case R.id.rl_balance:
				startActivity(new Intent(mContext, MyWalletBalanceActivity.class));
				break;
			case R.id.rl_jf_balance:
				startActivity(new Intent(mContext, MyPointBlanceActivity.class));
				break;
			case R.id.rl_device:
				startActivity(new Intent(mContext, MyDeviceListActivity.class));
				break;
			case R.id.rl_message:
//				startActivity(new Intent(mContext, MyMessageActivity.class));

				startActivity(new Intent(mContext,	MyConversationListActivity.class));

//				startActivity(new Intent(mContext, ChatMainActivity.class));
				break;
			case R.id.rl_operator:
	//			ToastUtils.showShort(this, "该功能暂未开通");
				startActivity(new Intent(mContext, MyOperatorManagerActivity.class));
				break;
			case R.id.rl_custom:
				startActivity(new Intent(mContext, MyCustomManagerActivity.class));
				break;
			case R.id.rl_qr:
				startActivity(new Intent(mContext, MyMchInviteActivity.class));
				break;
			case R.id.rl_profit:
				startActivity(new Intent(mContext, MyProfitActivity.class));
				break;
			case R.id.rl_login_setting:
				startActivity(new Intent(mContext, ActivityLoginPwdSetting.class));
				break;
			case R.id.rl_setting:
				MyDialog.Builder builder = new MyDialog.Builder(mContext);
				builder.setTitle("提示");
				builder.setMessage("确定退出登录吗？");
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
						dologout();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				});
				builder.create().show();

				break;
			default: break;
		}
	}

//	private void startChat(){
//
////		String userName
//		//设置要发送出去的昵称
//		SharedPreferencesUtils.setParam(this, APPConfig.USER_NAME,userName);
//		//设置要发送出去的头像
//		SharedPreferencesUtils.setParam(this,APPConfig.USER_HEAD_IMG,avatar[(int)(9.0*Math.random()) + 1]);
//
//		Intent intent=new Intent(this,MyChatActivity.class);
//		//传入参数
//		Bundle args=new Bundle();
//		args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
//		args.putString(EaseConstant.EXTRA_USER_ID,editText.getText().toString());
//		intent.putExtra("conversation",args);
//
//		startActivity(intent);
//	}


	private void dologout() {
		String url = Config.SBS_LOGOUT;
		RequestParams params = new RequestParams();
		params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");
		showProgress("加载中...");

		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@Override
			public void success(String response) {
				disShowProgress();
				try {

					JSONObject result = new JSONObject(response);
					if (!result.optBoolean("status")) {
						showErrorMsg(result.getString("message"));
						return;
					}
					MyApplication.getmInstance().setNull();
					SharedPreferencesUtils.setParam(mContext, APPConfig.USER_NAME,"");
					SharedPreferencesUtils.setParam(mContext, APPConfig.PASS_WORD,"");
					MyActivityManager.getInstance().logout(mContext);
					EMClient.getInstance().logout(true);
					PushServiceFactory.getCloudPushService().unbindAccount(new CommonCallback() {
						@Override
						public void onSuccess(String s) {
							LogUtils.e(TAG, s);
//							ToastUtils.showShort(mContext, s);
						}

						@Override
						public void onFailed(String s, String s1) {

						}
					});

				} catch (JSONException e) {
					e.printStackTrace();

				}
			}

			@Override
			public void failed(Throwable error) {
				showE404();
				disShowProgress();
			}
		});
	}


	private void getdata(){
		showProgress("加载中...");

		String url = Config.SBS_MY_INFO;
		RequestParams params = new RequestParams();
		params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid()+"");

		HttpUtil.get(url, params, new HttpUtil.RequestListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void success(String response) {
				disShowProgress();
				try {


					JSONObject result = new JSONObject(response);
					if (!result.optBoolean("status")) {
						showErrorMsg(result.getString("message"));
						return;
					}
					MySbsInfo sbsInfo = new Gson().fromJson(result.optString("data"), new TypeToken<MySbsInfo>(){}.getType());
					if (sbsInfo != null){
						ImageLoader.getInstance().displayImage(sbsInfo.getIcon(), img_user_icon);
						tv_name.setText(sbsInfo.getShopName());
						tv_time.setText("开店时间："+sbsInfo.getGmtCreate());
						tv_balance_amt.setText(StringUtils.formatIntMoney(sbsInfo.getBalance())+"元");
						tv_point_amt.setText(sbsInfo.getBonusSum()+"点");
						groupId = sbsInfo.getGroupId();
						userId = sbsInfo.getUserId();
					}

				} catch (Exception e) {
					e.getMessage();

				}
			}

			@Override
			public void failed(Throwable error) {
				disShowProgress();
				showE404();
			}
		});
	}
	
}
